﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using DDOS.Objects.Projectiles;
using DDOS.Objects.Effects;
using DDOS.Camera;

namespace DDOS.Objects
{
    class Enemy0 : Enemy
    {
        private Vector2 patrol_point;
        private Vector2 origin;
        Rectangle spriteRectangle, sourceRectangle1, sourceRectangle2, sourceRectangle3, sourceRectangle4;
        private const Decimal SPEED = 1m;
        int frame;
        int time;
        bool alarmed;
        Explosion explosion;
        Random ran;
        bool on_patrol;
        Room r;

        private BoundingCircle bounding_circle;
        public BoundingCircle boundingCircle
        {
            get { return bounding_circle; }
            set { bounding_circle = value; bounding_circle.parent = this; }
        }

        Gambit curGambit;
        int curGambitDepth;
        BoundingObject curGambitTarget;

        public BoundingCircle BoundingCircle
        {
            get { return boundingCircle; }
        }

        public void init_camera(Camera3D cam)
        {
            this.camera = cam;
        }

        public override void InitializeComponents()
        {
            gambits = new List<Gambit>();
            gambits.Add(new MeleeAttackGambit(Gambit.NearestAlly));
            curGambit = null;
            curGambitTarget = null;
            curGambitDepth = 1000;

            alarmed = true;
            this.maxHealth = 5;
            this.health = 5;
            this.power = 1;
            this.speed = 2;
            this.sight = 80;
            on_patrol = true;
            isActive = true;
            this.frame = ran.Next(0, 3);
            if (frame == 0)
            {
                time = 0;
            }
            if (frame == 1)
            {
                time = 12;
            }
            if (frame == 2)
            {
                time = 24;
            }
            if (frame == 3)
            {
                time = 36;
            }
            this.origin = new Vector2(this.coordinates.X, this.coordinates.Y);
            this.patrol_point = new Vector2(origin.X + ran.Next(-50, 50), origin.Y + ran.Next(50, 50));
            this.texture = ImageManager2D.Instance.Enemy;
            boundingObject = new BoundingRectangle((int)coordinates.X, (int)coordinates.Y, (int)coordinates.X + 25, (int)coordinates.Y + 25);
            boundingCircle = new BoundingCircle(coordinates.X, coordinates.Y, (float)this.sight);
            spriteRectangle = new Rectangle((int)coordinates.X, (int)coordinates.Y, 25, 25);
            sourceRectangle1 = new Rectangle(0, 0, 80, 64);
            sourceRectangle2 = new Rectangle(85, 0, 80, 64);
            sourceRectangle3 = new Rectangle(170, 0, 80, 64);
            sourceRectangle4 = new Rectangle(255, 0, 80, 64);
            this.velocity = new Vector2(0, 0);
            blocked = false;
        }

        /*public override void CollisionCallback(BoundingObject other, BoundingObject self)
        {
            Type otherType = other.parent.GetType();
            #region Check Agro Circle
            if (self == boundingCircle && typeof(Entity).IsAssignableFrom(otherType))
            {
                if (other == boundingObject)
                    return;
                for (int i = 0; i < gambits.Count && i <= curGambitDepth; i++)
                {
                    Gambit g = gambits[i];
                    if (g.CheckCondition(self.parent, other.parent) > 0)
                    {
                        if (i == curGambitDepth)
                        {
                            if (g.CheckCondition(self.parent, other.parent) > curGambit.CheckCondition(self.parent, curGambitTarget.parent))
                            {
                                curGambit = g;
                                curGambitDepth = i;
                                curGambitTarget = other;
                            }
                        }
                        else
                        {
                            curGambit = g;
                            curGambitDepth = i;
                            curGambitTarget = other;
                        }
                        break;
                    }
                }
            }
            #endregion
        }*/

        public void set_room(Room room)
        {
            r = room;
        }

        public void init_random(Random r)
        {
            ran = r;
        }

        private void patrol()
        {
            if ((this.coordinates - patrol_point).Length() >= (float)/*SPEED*/speed)
            {
                Vector2 v = (patrol_point - this.coordinates);
                v.Normalize();
                velocity = v * (float)speed;
            }
            else
            {
                this.patrol_point = new Vector2(origin.X + ran.Next(-50, 50), origin.Y + ran.Next(-50, 50));
            }
        }
  
        private void act()
        {
            /*if (curGambit != null)
            {
                curGambit.Activate(this, curGambitTarget.parent);
            }
            curGambit = null;
            curGambitTarget = null;
            curGambitDepth = 1000;*/
        }

        public override void Update(GameTime game_Time)
        {
            if (explosion != null)
            {
                if (explosion.active())
                {
                    explosion.Update(game_Time);
                    if (explosion.deleteMe == true)
                    {
                        CollisionEngine.Instance.Remove(explosion.boundingObject);
                        explosion = null;
                        //KILL THIS INSTANCE!
                    }
                }
            }
            if (isActive)
            {
                velocity = Vector2.Zero;
                if (on_patrol)
                {
                    patrol();
                }
                act();
                coordinates += velocity;

                this.boundingObject.Move(this.velocity.X, this.velocity.Y);
                this.boundingCircle.Move(this.velocity.X, this.velocity.Y);

                this.spriteRectangle.X = (int)this.coordinates.X;
                this.spriteRectangle.Y = (int)this.coordinates.Y;

                if (alarmed)
                {
                    time += 1;
                }
                if (health <= 0)
                {
                    r.add_to_score(100);
                    explosion = new Explosion(coordinates);
                    isActive = false;
                }
            }
        }

        public override void Draw()
        {
            if (explosion != null)
            {
                if (explosion.active())
                {
                    explosion.Draw();
                }
            }
            if (isActive)
            {
                if (alarmed)
                {
                    if (time <= 12)
                    {
                        ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle1, Color.White);
                    }
                    else if (time <= 24)
                    {
                        ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle2, Color.White);
                    }
                    else if (time <= 36)
                    {
                        ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle3, Color.White);
                    }
                    else if (time <= 48)
                    {
                        ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle4, Color.White);
                    }
                    else
                    {
                        ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle1, Color.White);
                        time = 0;
                    }
                }
                else
                {
                    ScreenManager.Instance.CurrentSpriteBatch.Draw(texture, spriteRectangle, sourceRectangle1, Color.White);
                }
            }
        }
    }
}
